package xyz.xpman.prisonerfitness.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.ListView;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.adapter.MovesAdapter;
import xyz.xpman.prisonerfitness.config.Constant;
import xyz.xpman.prisonerfitness.entity.Mappings;
import xyz.xpman.prisonerfitness.entity.Moves;
import xyz.xpman.prisonerfitness.util.StrKit;

@RequiresApi(api = Build.VERSION_CODES.O)
public class MovesActivity extends BaseActivity {
    private List<Moves> items = Collections.emptyList();
    private MovesAdapter movesAdapter;
    private ListView movesListView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moves);
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);


        initData();
        initView();
    }


    private void initData() {
        String id = getIntentExtra(Constant.EXTRA_CHAPTER_ID);

        setTitle(Mappings.getChapterTitle(id));

        if (StrKit.notBlank(id)) {
            items = new ArrayList<>(Mappings.getMoves(id));
        }
    }

    private void initView() {
        movesListView = findViewById(R.id.moves);

        movesAdapter = new MovesAdapter(this, R.layout.moves_temp, items);


        movesListView.setOnItemClickListener((parent, view, position, id) -> {
            Moves item = (Moves) movesAdapter.getItem(position);

            Intent intent = newIntent(TrainingGuideActivity.class);
            intent.putExtra(Constant.EXTRA_MOVES_ID, item.getId());
            startActivity(intent);
        });


        runOnUiThread(() -> movesListView.setAdapter(movesAdapter));
    }



    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.keyToBack(keyCode, event);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return processMenuItemForBack(item);
    }

    @Override
    protected void callBack() {
        this.finish();
    }

}